<?php

namespace app\api\model;

use think\Model;
use think\Db;
use think\db\Where;

class Room extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'ty_room';
    /**
     * 获取
     */
    public static function getRoomList($tch_id)
    {
        $where = array('house_id','=',$tch_id);
        $list =   Db::table('ty_room')
            ->field('r_id,house_id,room_num')
            ->where([$where])
            ->select();
        return $list;
    }
    public static function getAllList()
    {
        $list =   Db::table('ty_room')
            ->field('r_id,house_id,room_num,elec_meter')
            ->select();
        return $list;
    }
    public static function getId($name)
    {
        $where = [];
        $where['elec_meter'] = $name;
        $info = Db::table('ty_room')
            ->where($where)
            ->field('r_id,house_id,room_num,elec_meter')
            ->find();
        return $info;
    }
    public static function bindData($r_id,$data)
    {
        $where = array('r_id','=',$r_id);
        $res =   Db::table('ty_room')
            ->where([$where])
            ->update($data);
        return $res;
    }
    public static function getRoomInfo($name)
    {
        $where = [];
        $where['r.elec_meter'] = $name;
        $info = Db::table('ty_room')
            ->alias('r')
            ->join('ty_housing_resources h','r.house_id = h.hr_id')
            ->where($where)
            ->field('r.room_num,r.elec_meter,h.hr_id,h.hr_city,h.hr_rq_id,h.hr_entrance,h.hr_storey,h.hr_house_number')
            ->find();
        if(!$info){
            return false;
        }
        $rq_name = ResidentialQuarters::getRqName($info['hr_rq_id']);
        $info['con_house_gyxxdz'] = $info['hr_city'].'-'.$rq_name.'-'.$info['hr_entrance'].'-'.$info['hr_storey'].'-'.$info['hr_house_number'];
        unset($info['hr_id'],$info['hr_city'],$info['hr_rq_id'],$info['hr_entrance'],$info['hr_storey'],$info['hr_house_number']);
        return $info;
    }
    public static function getPublicRoom($h_id){
        $info = self::where(['house_id' => $h_id,'room_num' => 0])
            ->field('elec_meter,r_id')
            ->find();
        return $info;
    }
}
